package com.eric.windows.imgDispose;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposePathEffect;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.DiscretePathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathDashPathEffect;
import android.graphics.PathEffect;
import android.graphics.SumPathEffect;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.EView;

/**
 *
 * 这个View 主要测试Path用法，Path是一个非常有用的类，它可以将N个点连成一条"路径"，
 * 然后调用 drawPath函数，沿着"路径"绘制图形或文本。android 还提供了PathEffect来定义绘制效果，其子类有：
 * ComposePathEffect   混合效果
 * CornerPathEffect  边角效果
 * DashPathEffect 折线效果
 * DiscretePathEffect 发散效果
 * PathDashPathEffect
 * SumPathEffect 把两种效果组合一起， 组合效果  和 ComposePathEffect 区别是在 两种效果间隔显示，而ComposePathEffect把效果混合一起显示
 * <h3>Description</h3>
 * TODO
 * <h3>Author</h3> Eric
 * <h3>Date</h3> 2017/8/14 17:45
 * <h3>Copyright</h3> Copyright (c)2017 Shenzhen TLIT  Co., Ltd. Inc. All rights reserved.
 */

@EView
public class PathView extends View {
    /** 相位 对绘制效果略有影响*/
    float phase;
    PathEffect[] effects = new  PathEffect[7];
    int[] colors;
    Paint paint;
    Path  path;
    public PathView(Context context) {
        super(context);
    }

    public PathView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PathView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public PathView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    /**
     *  该函数会在初始化的时候调用
     */
    @AfterInject
    void  initData(){
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(4);//设置画笔宽度
        path = new Path();
        path.moveTo(0,0);
        for (int i = 0; i <= 15; i++) {
            //随机生成15个点
            path.lineTo(i*20,(float)Math.random()*60);
        }
        colors = new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW,Color.BLACK,Color.CYAN,Color.DKGRAY};

        effects[0] = null;
        effects[1] = new CornerPathEffect(10);
        effects[2] = new DiscretePathEffect(3,5);
        effects[3] = new DashPathEffect(new float[]{20,10,5,10},phase);
        Path path1 =  new Path();
        path1.addRect(0,0,8,8, Path.Direction.CCW);
        effects[4] = new PathDashPathEffect(path1,12,phase, PathDashPathEffect.Style.ROTATE);
        effects[5] = new ComposePathEffect(effects[2],effects[4]);
        effects[6] = new SumPathEffect(effects[3],effects[4]);

    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);

        for (int i = 0; i < effects.length; i++) {
            paint.setPathEffect(effects[i]);
            paint.setColor(colors[i]);
            canvas.drawPath(path,paint);
            canvas.translate(0,100);
        }

        ++ phase;
        invalidate();

    }
}
